package com.example.smartcity.ui.dashboard;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.CitySubwayLVAdapter;
import com.example.smartcity.ui.Bean.CSMessageBean;
import com.example.smartcity.ui.Bean.CitySubwayBean;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class CitySubway extends AppCompatActivity {

    private String TAG = "TAG";
    private TextView TVCurrent;
    private ListView LVRows;
    private Gson gson=null;
    private  ArrayList<CitySubwayBean.RowsBean> tempList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_subway);
        TVCurrent = findViewById(R.id.TV_Current);
        LVRows = findViewById(R.id.LV_Rows);
        inView();
    }
    public void setBack(View view){
        this.finish();
    }
    public void inView(){
        OkHttpUtils.getCall(OkHttpUtils.getBaseUrl(getBaseContext())+ "/metro/list?pageNum=1&pageSize=10&currentName=建国门").enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d(TAG, "onResponse: "+"asd");
                gson = new Gson();
                tempList = new ArrayList<>();
                String Json = response.body().string();
                CitySubwayBean bean = gson.fromJson(Json,CitySubwayBean.class);
                if (bean.getCode()==200){
                    List<CitySubwayBean.RowsBean> list = bean.getRows();
                    LVRows.post(new Runnable() {
                        @Override
                        public void run() {
                            TVCurrent.setText(list.get(0).getCurrentName());
                            LVRows.setAdapter(new CitySubwayLVAdapter(getBaseContext(),R.layout.citysubwaylayout,list));
                        }
                    });
                    if (list!=null){
                        tempList.addAll(list);
                    }
                }
            }
        });
        LVRows.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int LineId = tempList.get(position).getLineId();
                ArrayList<String> lines = new ArrayList<>();
                ArrayList<CSMessageBean.DataBean.MetroStepsListBean> tempList = new ArrayList<>();
                OkHttpUtils.getCall(OkHttpUtils.getBaseUrl(getBaseContext())+"/metro/"+LineId).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d(TAG, "onFailure: "+e.getMessage());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String Json = response.body().string();
                        CSMessageBean bean= gson.fromJson(Json,CSMessageBean.class);
                        CSMessageBean.DataBean beanData= bean.getData();
                        List<CSMessageBean.DataBean.MetroStepsListBean> list=beanData.getMetroStepsList();
                        if (list!=null){
                            tempList.addAll(list);
                        }
                        for (int i = 0; i < tempList.size(); i++) {
                            lines.add(tempList.get(i).getName());
                        }
                        Intent intent = new Intent(CitySubway.this,CitySubwayMessage.class);
                        intent.putStringArrayListExtra("list",lines);
                        intent.putExtra("station",beanData.getStationsNumber()+"站/"+beanData.getKm()+"Km");
                        startActivity(intent);
                    }
                });
            }
        });
    }
}